﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common
{
    public class ToolsGis
    {
        /// <summary>
        /// 经纬度转墨卡托
        /// </summary>
        /// <param name="lonLat"></param>
        /// <returns></returns>
        public static GisPoint lonLat2Mercator(GisPoint lonLat)
        {
            GisPoint mercator = new GisPoint();
            double x = lonLat.X * 20037508.34 / 180;
            double y = Math.Log(Math.Tan((90 + lonLat.Y) * Math.PI / 360)) / (Math.PI / 180);
            y = y * 20037508.34 / 180;
            mercator.X = x;
            mercator.Y = y;
            return mercator;
        }

        /// <summary>
        /// 墨卡托转经纬度
        /// </summary>
        /// <param name="mercator"></param>
        /// <returns></returns>
        public static GisPoint Mercator2lonLat(GisPoint mercator)
        {
            GisPoint lonLat = new GisPoint();
            double x = mercator.X / 20037508.34 * 180;
            double y = mercator.Y / 20037508.34 * 180;
            y = 180 / Math.PI * (2 * Math.Atan(Math.Exp(y * Math.PI / 180)) - Math.PI / 2);
            lonLat.X = x;
            lonLat.Y = y;
            return lonLat;
        }
    }

    public class GisPoint
    {
        private double x;

        public double X
        {
            get { return x; }
            set { x = value; }
        }
        private double y;

        public double Y
        {
            get { return y; }
            set { y = value; }
        }

        public GisPoint() { }
        public GisPoint(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
    }
}
